package com.hwadee.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hwadee.property.mapper.AdministratorMapper;
import com.hwadee.property.pojo.Administrator;
import com.hwadee.property.service.IAdministratorService;
import com.hwadee.property.util.BusinessException;
import com.hwadee.property.util.DateUtil;
import com.hwadee.property.util.ErrorCodes;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 管理员服务实现类
 */
@Service
public class AdministratorServiceImpl extends ServiceImpl<AdministratorMapper, Administrator> implements IAdministratorService {

    @Override
    public Administrator getAdminByName(String adminName) {
        // 校验管理员名称不能为空
        if (adminName == null || adminName.trim().isEmpty()) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "管理员名称不能为空");
        }

        // 使用MyBatis-Plus的查询条件构造器查询管理员
        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_name", adminName);
        Administrator admin = this.getOne(queryWrapper);

        // 如果管理员不存在，抛出业务异常
        if (admin == null) {
            throw new BusinessException(ErrorCodes.NOT_FOUND, "管理员不存在");
        }
        return admin;
    }

    @Override
    public boolean updatePassword(Long adminId, String newPassword) {
        // 校验管理员ID是否合法
        if (adminId == null || adminId <= 0) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "无效的管理员ID");
        }
        // 校验新密码不能为空
        if (newPassword == null || newPassword.trim().isEmpty()) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "新密码不能为空");
        }

        // 查询管理员信息
        Administrator admin = this.getById(adminId);

        // 如果管理员不存在，抛出业务异常
        if (admin == null) {
            throw new BusinessException(ErrorCodes.NOT_FOUND, "管理员不存在");
        }

        // 更新密码并记录更新时间
        admin.setAdminPassword(newPassword);
        admin.setUpdateTime(DateUtil.dateToString(new Date()));
        return this.updateById(admin);
    }

    @Override
    public List<Administrator> getAllAdministrators() {
        // 查询所有管理员信息
        return this.list();
    }

    @Override
    public Administrator doLogin(Administrator administrator) {
        // 检查用户对象是否为空
        if (ObjectUtils.isEmpty(administrator)) {
            return null;
        }

        // 检查用户名和密码是否为空
        if (!StringUtils.hasLength(administrator.getAdminName()) || !StringUtils.hasLength(administrator.getAdminPassword())) {
            return null;
        }

        // 使用 LambdaQueryWrapper 查询用户名
        LambdaQueryWrapper<Administrator> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Administrator::getAdminName, administrator.getAdminName());

        // 查询用户
        List<Administrator> administrators = baseMapper.selectList(queryWrapper);
        if (administrators == null || administrators.isEmpty()) {
            return null;
        }

        // 获取查询结果中的第一个用户
        Administrator bean = administrators.get(0);

        // 检查密码是否匹配
        if (administrator.getAdminPassword().equals(bean.getAdminPassword())) {
            return bean;
        }

        return null;
    }
}
